package org.xxx.epms.gateway.security.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.xxx.epms.common.utils.JwtTokenUtil;
import org.xxx.epms.gateway.config.service.AdminDetailsService;
import org.xxx.epms.gateway.config.service.InspectorDetailsService;
import org.xxx.epms.gateway.security.component.DynamicSecurityMetadataSource;
import org.xxx.epms.gateway.security.filter.JwtAuthenticationFilter;

import java.util.stream.Collectors;

@Configuration
public class CommonSecurityConfig {
    @Bean
    public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); }

    @Bean
    public JwtTokenUtil jwtTokenUtil() { return new JwtTokenUtil(); }

    @Bean
    public AdminDetailsService adminDetailsService() { return new AdminDetailsService(); }

    @Bean
    public InspectorDetailsService inspectorDetailsService() { return new InspectorDetailsService(); }

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }

    @Bean
    public JwtAuthenticationFilter jwtAuthenticationFilter() {
        return new JwtAuthenticationFilter();
    }

    @Bean
    public DynamicSecurityMetadataSource dynamicSecurityMetadataSource() { return new DynamicSecurityMetadataSource(); }

    @Bean
    public IgnoreUrlsConfig ignoreUrlsConfig() { return new IgnoreUrlsConfig(); }
}
